import { WebSocketClient, type WebSocketOptions } from "../utils/websocket";

/**
 * websocket 连接组合式函数
 * @author lianqq
 * @param options 
 * @returns 
 */
export const useWebSocket = (options: WebSocketOptions) => {
  const wsClient = ref<WebSocketClient>()

  const connect = () => {
    wsClient.value = WebSocketClient.getInstance(options)
  }

  const disconnect = () => {
    wsClient.value?.close()
  }

  const send = (message: string | ArrayBufferLike | Blob | ArrayBufferView) => {
    wsClient.value?.send(message)
  }


  onUnmounted(() => {
    disconnect()
  })

  return {
    connect,
    disconnect,
    send,
    wsClient
  }
}